package com.wangzhen.codebase.paocha;

import java.math.BigDecimal;
import java.util.concurrent.*;

public class PriceDemo {

    static class JDPrice implements Callable<BigDecimal> {
        private final Integer itemId;
        private final CountDownLatch countDownLatch;

        public JDPrice(Integer itemId, CountDownLatch latch) {
            this.itemId = itemId;
            this.countDownLatch = latch;
        }
        private final ThreadLocalRandom random = ThreadLocalRandom.current();
        @Override
        public BigDecimal call() throws Exception {
            int i = random.nextInt(100);
            //countDownLatch.countDown();
            TimeUnit.SECONDS.sleep(2);
            System.out.printf("threadeName=%s, jdPrice=%d,time=%d \n", Thread.currentThread().getName(), i, System.currentTimeMillis());
            return new BigDecimal(i);
        }
    }

    static class TMPrice implements Callable<BigDecimal> {
        private final Integer itemId;
        private final CountDownLatch countDownLatch;

        public TMPrice(Integer itemId, CountDownLatch latch) {
            this.itemId = itemId;
            this.countDownLatch = latch;
        }
        private final ThreadLocalRandom random = ThreadLocalRandom.current();
        @Override
        public BigDecimal call() throws Exception {
            int i = random.nextInt(100);
            //countDownLatch.countDown();
            TimeUnit.SECONDS.sleep(1);
            System.out.printf("threadeName=%s, tmPrice=%d, time=%s\n", Thread.currentThread().getName(), i, System.currentTimeMillis());
            return new BigDecimal(i);
        }
    }

    static class PDDPrice implements Callable<BigDecimal> {
        private final Integer itemId;
        private final CountDownLatch countDownLatch;

        public PDDPrice(Integer itemId, CountDownLatch latch) {
            this.itemId = itemId;
            this.countDownLatch = latch;
        }
        private final ThreadLocalRandom random = ThreadLocalRandom.current();
        @Override
        public BigDecimal call() throws Exception {
            int i = random.nextInt(100);
            TimeUnit.SECONDS.sleep(2);
            System.out.printf("threadeName=%s, pddPrice=%d, time=%s\n", Thread.currentThread().getName(), i, System.currentTimeMillis());
            //countDownLatch.countDown();
            return new BigDecimal(i);
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<BigDecimal> jdTask = new FutureTask<>(new JDPrice(1, null));
        FutureTask<BigDecimal> tmTask = new FutureTask<>(new TMPrice(1, null));
        FutureTask<BigDecimal> pddTask = new FutureTask<>(new PDDPrice(1, null));

        Thread t1 = new Thread(jdTask);
        Thread t2 = new Thread(tmTask);
        Thread t3 = new Thread(pddTask);

        t1.start();t2.start();t3.start();
        BigDecimal jdPrice = jdTask.get();
        BigDecimal tmPrice = tmTask.get();
        BigDecimal pddPrice = pddTask.get();

        System.out.println("jdPrice="+jdPrice);
        System.out.println("tmPrice="+tmPrice);
        System.out.println("pddPrice="+pddPrice);


        ExecutorService executorService = Executors.newFixedThreadPool(3);
        Future<BigDecimal> jdFuture = executorService.submit(new JDPrice(1, null));
        Future<BigDecimal> tmFuture = executorService.submit(new TMPrice(1, null));
        Future<BigDecimal> pddFuture = executorService.submit(new PDDPrice(1, null));



    }


}
